package com.melon.http;

import com.fasterxml.jackson.core.type.TypeReference;
import com.melon.entity.PowerAssistParam;
import com.melon.entity.Response;
import com.melon.entity.PowerAssistQueryParam;
import com.melon.entity.PowerAssistServiceInfoVM;
import com.melon.util.RandomUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.util.Objects;

/**
 * @author TianGua
 * @since 2024/10/17 - 11:32
 */
@Component
@Slf4j
public class PowerAssistRemoteHandler extends AbstractHttpRemoteHandler {

    private String queryServiceInfoPath = "/v1/power";

    private String powerAssistPath = "/v1/power/assist";

    public PowerAssistServiceInfoVM getAppServiceInfo(String appId, String appSecret) {
        try {
            PowerAssistQueryParam queryParam = new PowerAssistQueryParam();

            queryParam.setAppId(appId);
            String nonce = RandomUtil.shortUUID(10);
            queryParam.setNonce(nonce);

            log.info("【getAppServiceInfo】query app service info start. appId = [{}], nonce = [{}]", appId, nonce);

            String deployAddress = getDeployAddress();
            if(StringUtils.isBlank(deployAddress)) {
                log.info("【getAppServiceInfo】query app service info finish. deploy address is null. appId = [{}], nonce = [{}]", appId, nonce);
                return null;
            }

            String deployPort = getDeployPort();
            if(StringUtils.isBlank(deployPort)) {
                log.info("【getAppServiceInfo】query app service info finish. deploy port is null. appId = [{}], nonce = [{}]", appId, nonce);
                return null;
            }

            String url = "http://" + deployAddress + ":" + deployPort + queryServiceInfoPath;
            log.info("【getAppServiceInfo】query app service info running. appId = [{}], nonce = [{}], url = [{}]", appId, nonce, url);

            String result = doGet(appSecret, url, queryParam);

            Response<PowerAssistServiceInfoVM> response = objectMapper.readValue(result, new TypeReference<>() {});
            if(Objects.isNull(response)) {
                log.info("【getAppServiceInfo】query app service info finish. response is null.");
                return null;
            }

            Integer status = response.getStatus();
            if(!Objects.equals(status, 200)) {
                log.info("【getAppServiceInfo】query app service info finish. response's status is not 200. msg = [{}]", response.getMessage());
                return null;
            }

            PowerAssistServiceInfoVM data = response.getData();
            if(Objects.isNull(data)) {
                log.info("【getAppServiceInfo】query app service info finish. response's data is null.");
                return null;
            }

            log.info("【getAppServiceInfo】query app service info finish. result = [{}]", objectMapper.writeValueAsString(data));

            return data;
        } catch (Exception exception) {
            log.info("【getAppServiceInfo】query app service info error. appId = [{}]", appId, exception);
            return null;
        }
    }

    public Boolean expand(String appId, String appSecret, Integer count) {
        try {
            PowerAssistParam param = new PowerAssistParam();
            param.setAppId(appId);

            String nonce = RandomUtil.shortUUID(10);
            param.setNonce(nonce);
            log.info("【expand】expand start. appId = [{}], nonce = [{}]", appId, nonce);

            param.setAmount(count);

            param.setType(1);

            String deployAddress = getDeployAddress();
            if(StringUtils.isBlank(deployAddress)) {
                log.info("【expand】expand finish. deploy address is null. appId = [{}], nonce = [{}]", appId, nonce);
                return null;
            }

            String deployPort = getDeployPort();
            if(StringUtils.isBlank(deployPort)) {
                log.info("【expand】expand finish. deploy port is null. appId = [{}], nonce = [{}]", appId, nonce);
                return null;
            }

            String url = "http://" + deployAddress + ":" + deployPort + powerAssistPath;
            log.info("【expand】expand running. appId = [{}], nonce = [{}], url = [{}]", appId, nonce, url);

            String result = doPost(appSecret, url, param);
            log.info("【expand】result = [{}]", result);

        } catch (Exception exception) {
            log.info("【expand】expand error. appId = [{}]", appId, exception);
            return Boolean.FALSE;
        }

        return Boolean.TRUE;
    }

    public Boolean contract(String appId, String appSecret, Integer count) {
        try {
            PowerAssistParam param = new PowerAssistParam();
            param.setAppId(appId);

            String nonce = RandomUtil.shortUUID(10);
            param.setNonce(nonce);
            log.info("【contract】contract start. appId = [{}], nonce = [{}]", appId, nonce);

            param.setAmount(count);

            param.setType(2);

            String deployAddress = getDeployAddress();
            if(StringUtils.isBlank(deployAddress)) {
                log.info("【contract】contract finish. deploy address is null. appId = [{}], nonce = [{}]", appId, nonce);
                return null;
            }

            String deployPort = getDeployPort();
            if(StringUtils.isBlank(deployPort)) {
                log.info("【contract】contract finish. deploy port is null. appId = [{}], nonce = [{}]", appId, nonce);
                return null;
            }

            String url = "http://" + deployAddress + ":" + deployPort + powerAssistPath;
            log.info("【contract】contract running. appId = [{}], nonce = [{}], url = [{}]", appId, nonce, url);

            String result = doPost(appSecret, url, param);
            log.info("【contract】result = [{}]", result);

        } catch (Exception exception) {
            log.info("【contract】contract error. appId = [{}]", appId, exception);
            return Boolean.FALSE;
        }

        return Boolean.TRUE;
    }

}
